﻿@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService

@inject IHttpService _httpService
@inject IPopupService PopupService

<CnGalWebSite.Components.Containers.MasaTitleContainer Title="群聊QQ号" Icon="@IconType.QQ.ToIconString()" Class="h-100">
    <div style="display: flex; flex-direction: column; justify-content: space-between;">
        @if (string.IsNullOrWhiteSpace(Model.GroupQQ))
        {
            <span style="font-size: small;" class="pb-2">绑定QQ号可以向看板娘查询信息，点击绑定获取身份识别码，并在群聊中发送【看板娘，绑定+身份识别码】即可绑定</span>
        }
        else
        {
            <span style="font-size: small;" class="pb-2">QQ号：@Model.GroupQQ</span>

    }


    <div style="display: flex; justify-content: flex-end;">
        <div>
            <MButton Text Block  OnClick="OnClickBind" Color="primary">
                <MIcon Left>@(string.IsNullOrWhiteSpace(Model.GroupQQ) ? "mdi-link-variant" : "mdi-link-variant-plus ")</MIcon>
                @(string.IsNullOrWhiteSpace(Model.GroupQQ) ? "绑定" : "更换绑定")
            </MButton>
        </div>
        @if (string.IsNullOrWhiteSpace(Model.GroupQQ) == false)
        {
            <div>
                <MButton Text Block  OnClick="OnClickUnBind" Color="primary">
                    <MIcon Left>mdi-share-all-outline</MIcon>
                    解除绑定
                </MButton>
            </div>
        }


    </div>
    </div>
</CnGalWebSite.Components.Containers.MasaTitleContainer>
<CnGalWebSite.Shared.MasaComponent.Shared.Components.HumanMachineVerification @ref="humanMachineVerification" OnVerficated="OnVerficated" />
<MDialog @bind-Value="Dialog"
         Width="600">
    <MDCard>
        <MCardTitle Class="text-h5">
            身份识别码
        </MCardTitle>
        <MCardText>
            在群聊中发送【看板娘，绑定+身份识别码】即可绑定，90分钟内有效，仅可使用一次
        </MCardText>
        <MCardText>
            <PCopyableText>
                @("看板娘，绑定 "+ Code)
            </PCopyableText>
        </MCardText>
        <MCardText>
            <PCopyableText>
                @Code
            </PCopyableText>
        </MCardText>
        <MCardActions Class="pb-6 pe-6 ps-6 pt-6">
            <MSpacer></MSpacer>
            <CnGalWebSite.Components.Buttons.MasaButton Text="确定" IsAsync="true" OnClick="() => Dialog = false" Icon="mdi-check" Color="success" TextStyle />
        </MCardActions>
    </MDCard>
</MDialog>
@code {
    [Parameter]
    public EditUserDataViewModel Model { get; set; } = new EditUserDataViewModel();
    [Parameter]
    public EventCallback OnChanged { get; set; }
    bool Dialog { get; set; }

    CnGalWebSite.Shared.MasaComponent.Shared.Components.HumanMachineVerification humanMachineVerification;
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    string Code;
    bool bind;
    public async Task OnClickUnBind()
    {
        bind = false;
        await humanMachineVerification.StartVerification();
    }
    public async Task OnClickBind()
    {
        bind = true;
        await humanMachineVerification.StartVerification();
    }
    public async Task OnVerficated(HumanMachineVerificationResult model)
    {
        if (model.Success == false)
        {
            return;
        }

        if(bind)
        {
            await ProcBind(model);
        }
        else
        {
            await ProcUnBind(model);
        }

    }
    public async Task ProcUnBind(HumanMachineVerificationResult model)
    {
        //请求数据
        try
        {

            var obj = await _httpService.PostAsync<UnBindGroupQQModel, Result>("api/space/UnBindGroupQQ", new UnBindGroupQQModel
            {
                Verification = model
            });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("解除绑定失败", obj.Error);
            }
            else
            {
                await PopupService.ToastSuccessAsync("解除绑定成功", "解除绑定成功");
                Model.GroupQQ = null;
                await OnChanged.InvokeAsync();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "解除绑定失败");
        }
    }
    public async Task ProcBind(HumanMachineVerificationResult model)
    {
        //请求数据
        try
        {

            var obj = await _httpService.PostAsync<UnBindGroupQQModel, Result>("api/space/GetBindGroupQQCode", new UnBindGroupQQModel
            {
                Verification = model
            });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("获取身份识别码失败", obj.Error);
            }
            else
            {
                Code = obj.Error;
                Dialog = true;
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取身份识别码失败");
        }
    }
}
